package com.example.healthapp.ui.fragment;

import android.app.ActivityManager;
import android.app.AlertDialog;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.example.healthapp.R;
import com.example.healthapp.service.NotificationService;
import com.example.healthapp.ui.activity.HeartRateActivity;
import com.example.healthapp.ui.activity.HeightActivity;
import com.example.healthapp.ui.activity.SleepActivity;
import com.example.healthapp.ui.activity.WalkCountActivity;
import com.example.healthapp.ui.activity.WeightActivity;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.MainActivitys;

import java.util.List;


// 功能页面的Fragment
public class FunctionFragment extends Fragment {

    View view;
    CardView fgt3_heart_rate, fgt3_private, fgt3_yuan_health, fgt3_height, fgt3_weight, fgt3_sleep, fgt3_walk;
    TextView fgt3_health_notification;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_function, container, false);
            initView();
            event();
        }
        return view;
    }


    private void event() {
        fgt3_walk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(getContext(), WalkCountActivity.class);
            }
        });
        fgt3_sleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(getContext(), SleepActivity.class);
            }
        });
        fgt3_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(getContext(), HeightActivity.class);
            }
        });
        fgt3_weight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(getContext(), WeightActivity.class);
            }
        });
        fgt3_heart_rate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InterfaceTool.setIntent(getContext(), HeartRateActivity.class);
            }
        });
        fgt3_yuan_health.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(getContext()).setTitle("关于健康e生").setMessage("" +
                        "\n希望您幸福快乐，健康一生").create();
                alertDialog.show();
            }
        });
        fgt3_private.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite("https://ask.dxy.com/index#/list/section/60?dxa_adplatform=m.dxy.com");
            }
        });
    }

    //初始化UI
    private void initView() {
        fgt3_private = view.findViewById(R.id.fgt3_private_doctor);
        fgt3_yuan_health = view.findViewById(R.id.fgt3_yuan_health);
        fgt3_heart_rate = view.findViewById(R.id.fgt3_heart_rate);
        fgt3_height = view.findViewById(R.id.fgt3_height);
        fgt3_weight = view.findViewById(R.id.fgt3_weight);
        fgt3_sleep = view.findViewById(R.id.fgt3_sleep);
        fgt3_walk = view.findViewById(R.id.fgt3_walk);
    }

    public boolean isServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);
        for (ActivityManager.RunningServiceInfo service : runningServices) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    //转跳到外部网页
    public void openWebsite(String url) {
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        try {
            startActivity(intent);
        } catch (Exception e) {
            // 处理没有浏览器应用的情况
            Toast.makeText(getContext(), "没有可用的浏览器应用", Toast.LENGTH_SHORT).show();
        }
    }
}